package com.jogger.newprint.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jogger.newprint.bean.BlueTooth;

import java.util.List;

/**
 * Created by jogger on 2017/10/13.
 * 蓝牙
 */

public class MyBlueToothAdapter extends RecyclerView.Adapter<MyBlueToothAdapter.MyViewHolder> {
    private List<BlueTooth> mBlueTooths;
    private Context mContext;
    private OnItemClickListener mListener;

    public MyBlueToothAdapter(Context context) {
        mContext = context;
    }

    public interface OnItemClickListener {
        void onItemClick(View v, BlueTooth blueTooth);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(mContext).inflate(android.R.layout.simple_expandable_list_item_1, parent, false));
    }

    public void loadData(List<BlueTooth> blueTooths) {
        mBlueTooths = blueTooths;
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        holder.tv_name.setText(mBlueTooths.get(position).getmName());
        holder.itemView.setTag(position);
        if (mListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(v, mBlueTooths.get((Integer) holder.itemView.getTag()));
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mBlueTooths.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv_name;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv_name = (TextView) itemView.findViewById(android.R.id.text1);
        }
    }
}
